package com.yanqu.road.dao.impl.wingroom;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.wingroom.UserWingRoomShareEvent;
import com.yanqu.road.entity.wingroom.WingRoomEventReward;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2022/3/11 1:22
 */
public class UserWingRoomShareEventDaoImpl extends TempDataObjectDao<UserWingRoomShareEvent> {
    public static String insertSql = "INSERT INTO t_u_wingroom_shareevent(userId,lastUpdateTime,judgeFailTimes," +
            "triggerTime,status,getRewardTime,rewardMap,shareUnionUid) values(?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserWingRoomShareEvent data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getJudgeFailTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTriggerTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStatus()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGetRewardTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRewardMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getShareUnionUid()));

        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE t_u_wingroom_shareevent set lastUpdateTime=?, judgeFailTimes=?, triggerTime=?," +
            "status=?, getRewardTime=?, rewardMap=?, shareUnionUid=? where userId=?";

    @Override
    protected boolean updateData(UserWingRoomShareEvent data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getJudgeFailTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTriggerTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStatus()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGetRewardTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRewardMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getShareUnionUid()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserWingRoomShareEvent parseData(ResultSet rs) throws SQLException {
        UserWingRoomShareEvent data = new UserWingRoomShareEvent();
        data.setUserId(rs.getLong("userId"));
        data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        data.setJudgeFailTimes(rs.getInt("judgeFailTimes"));
        data.setTriggerTime(rs.getLong("triggerTime"));
        data.setStatus(rs.getInt("status"));
        data.setGetRewardTime(rs.getLong("getRewardTime"));
        data.setRewardMap(JSONObject.parseObject(rs.getString("rewardMap"), new TypeReference<Map<Integer, WingRoomEventReward>>() {
        }));
        data.setShareUnionUid(rs.getString("shareUnionUid"));
        return data;
    }

    public static String selectSql = "SELECT * FROM t_u_wingroom_shareevent";

    public Map<Long, UserWingRoomShareEvent> getUserWingRoomShareEventMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, UserWingRoomShareEvent> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            rs = call.executeQuery();

            while (rs.next()) {
                UserWingRoomShareEvent data = parse(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
